package cn.com.gridinfo.sooc.core.my.msg.dao;
import com.jeremy.arad.Arad;

import net.tsz.afinal.db.sqlite.DbModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.com.gridinfo.sooc.core.my.msg.bean.PushNotice;

/**
 * 消息通告DAO
 * Created by jieli on 15/8/6.
 */
public class NoticeDao {

    public NoticeDao(){
        super();
    }

    /**
     * 保存消息公告
     * @param noticeEntity
     */
    public  void saveNotice(PushNotice noticeEntity){
        Arad.db.save(noticeEntity);
    }

    /**
     * 删除消息公告
     * @param id
     */
    public void deleteNotice(int id){
        Arad.db.deleteById(PushNotice.class, id + "");
    }

    /**
     * 更新通知
     * @param noticeEntity
     */
    public void updateNotice(PushNotice noticeEntity){
        Arad.db.update(noticeEntity);
    }


    /**
     * 分页查询消息公告 按照某個條件倒敘
     * @param whereStr 條件查詢 aa=aa
     * @param pageNo 當前頁
     * @param pageSize 每页显示条数
     * @return
     */
    public List<HashMap<String,Object>> queryListByPage(String whereStr , int pageNo,int pageSize){

        List<DbModel> dbModelList = null;
        List<HashMap<String,Object>> noticeList = new ArrayList<HashMap<String,Object>>();
        try {
            dbModelList = Arad.db.findAllBySql(PushNotice.class, whereStr,"createTime",pageNo, pageSize);
            if(dbModelList!=null && !dbModelList.isEmpty())
                for (DbModel dbModel : dbModelList){

                    HashMap<String,Object> map = dbModel.getDataMap();
//                Date dateTime = Date.valueOf(map.get("createTime").toString());
//                map.put("createTime", DateUtil.getTimeAgo(dateTime));

                    noticeList.add(map);
                }
        }catch (Exception e){
            return noticeList ;
        }

        return noticeList;
    }


    /**
     * 获取最新的一条推送通知
     * @param whereStr
     * @return
     */
    public HashMap<String,Object> queryLatestNotice(String whereStr){
        List<DbModel> dbModelList = null;
        HashMap<String,Object> noticeinfo = new HashMap<>();
        List<HashMap<String,Object>> noticeList = new ArrayList<HashMap<String,Object>>();
        try {
            dbModelList = Arad.db.findAllBySql(PushNotice.class, whereStr,"createTime",1, 1);
            if(dbModelList!=null && !dbModelList.isEmpty())
                for (DbModel dbModel : dbModelList){
                    HashMap<String,Object> map = dbModel.getDataMap();
                    noticeList.add(map);
                }
        }catch (Exception e){
        }
        if(noticeList.size() ==1){
            noticeinfo = noticeList.get(0);
            return noticeinfo;
        }else{
            return noticeinfo;
        }
    }


    /**
     * 查询所有
     * @return
     */
    public int getTotalCount(String suid){
        try {
            return  Arad.db.countByWhere(PushNotice.class,"1=1 and suid = '"+suid+"'");
        }catch (Exception e){
            return 0;
        }
    }

    /**
     * 查询未读消息数量
     * @return
     */
    public int getNoReadCout(String suid){
        try {
            return Arad.db.countByWhere(PushNotice.class,"status=0 and suid = '"+suid+"' ");
        }catch (Exception e){
            return 0;
        }

    }

    /**
     * 查询消息公告信息
     * @param id
     * @return
     */
    public PushNotice getNoticeInfo(int id){
       return Arad.db.findById(PushNotice.class,id);
    }
}
